package com.lysj.bhp.user.service;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.lysj.bhp.user.base.UserContent;
import com.lysj.bhp.user.domain.BatchPlayCoin;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.user.domain.WalletEditLog;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zk
 * @description
 * @create 2018-07-31 14:25
 **/
@Service
public class BatchPlayCoinService extends UserContent {

    public static List<BatchPlayCoin> batchPlayCoinList = new ArrayList<>();

    @Transactional
    public Resp excelImport(MultipartFile file) throws Exception {
        if (file.getSize() == 0) {
            return new Resp(Resp.PARAM_ERROR, "文件内容为空，请重新选择文件！");
        }

        //导入之前先将之前的数据清空，防止之前的数据会遗留到本次导入的数据中
        batchPlayCoinList = new ArrayList<>();
        //将倒入的数据封装成对象存入到集合中
        List<BatchPlayCoin> batchPlayCoinLists = null;
        try {
            batchPlayCoinLists = ExcelImportUtil.importExcel(file.getInputStream(), BatchPlayCoin.class, new ImportParams());
        } catch (Exception e) {
            return new Resp(Resp.PARAM_ERROR, "文件错误");
        }
        //遍历导入的数据






        for (int i = 0; i < batchPlayCoinLists.size(); i++) {
            //获取每一条数据
            BatchPlayCoin batchPlayCoin = batchPlayCoinLists.get(i);
            //设置数据的ID
            batchPlayCoin.setBatchPlayCoinId((long) (i + 1));
            //设置默认的打币状态
            batchPlayCoin.setCoinStatus("未打币");
            //下面是对导入数据的判空
            if (ParamUtil.isBlack(batchPlayCoin.getUserEmail())) {
                return new Resp(Resp.PARAM_ERROR, "第" + (i + 2) + "行,会员邮箱不能为空");
            }
            if (ParamUtil.isBlack(batchPlayCoin.getCoinType())) {
                return new Resp(Resp.PARAM_ERROR, "第" + (i + 2) + "行,币种不能为空");
            }
            if (batchPlayCoin.getCoinSum() == null) {
                return new Resp(Resp.PARAM_ERROR, "第" + (i + 2) + "行,数量不能为空");
            } else {
                //打币的数量要大于0
                if (batchPlayCoin.getCoinSum().compareTo(new BigDecimal(BigInteger.ZERO)) != 1) {
                    return new Resp(Resp.PARAM_ERROR, "第" + (i + 2) + "行,数量要大于0");
                }
            }
            if (ParamUtil.isBlack(batchPlayCoin.getOperation())) {
                return new Resp(Resp.PARAM_ERROR, "第" + (i + 2) + "行,操作不能为空");
            }
            //判断都没错的情况下，就加入之前定义的静态List中
            batchPlayCoinList.add(batchPlayCoin);
        }
        return new Resp("上传成功");
    }

    /**
     * @author wtl
     * @date 2018-08-06 9:16
     * @Description 添加打币记录
     */
    public void saveBatchPlayRecord(BatchPlayCoin model, User user) {
        WalletEditLog walletEditLog = new WalletEditLog();
        walletEditLog.setUserId(user.getUserId());
        walletEditLog.setEmail(model.getUserEmail());
        walletEditLog.setEditDate(new Date());
        if (model.getOperation().equals("减少")) {
            walletEditLog.setOperation(2);
        } else {
            walletEditLog.setOperation(1);
        }
        if (model.getCoinType().equals("bhpc")) {
            walletEditLog.setWalletType(100);
        } else if (model.getCoinType().equals("eth")) {
            walletEditLog.setWalletType(20);
        } else if (model.getCoinType().equals("btc")) {
            walletEditLog.setWalletType(10);
        }else if (model.getCoinType().equals("bhpcBouns")) {
            walletEditLog.setWalletType(110);
        }
        walletEditLog.setAmount(model.getCoinSum());
        walletEditLog.setReamrk("打币成功");
        walletEditLog.setOperationName(currentStaff().getRealname());
        walletEditLogRepository.save(walletEditLog);
    }
}
